import java.util.*;

class MyTimer{
    private PriorityQueue<MyTimerTask> priorityQueue = new PriorityQueue<>();
    public MyTimer(){
        Thread t = new Thread(() -> {
            while(true){
                synchronized (this){
                    try{
                        while(priorityQueue.isEmpty()){
                            this.wait();
                        }
                        MyTimerTask myTimerTask = priorityQueue.peek();
                        long curTime = System.currentTimeMillis();
                        if(curTime >= myTimerTask.getTime()){
                            myTimerTask.getRunnable().run();
                            priorityQueue.poll();
                        }else {
                            this.wait(myTimerTask.getTime() - curTime);
                        }
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }
    public void schedule(Runnable runnable, long delay){
        synchronized (this){
            MyTimerTask myTimerTask = new MyTimerTask(runnable,delay);
            priorityQueue.offer(myTimerTask);
            this.notify();
        }
    }
}
class MyTimerTask implements Comparable<MyTimerTask>{
    private Runnable runnable;
    private long time;
    public MyTimerTask(Runnable runnable, long delay){
        this.time = System.currentTimeMillis() + delay;
        this.runnable = runnable;
    }
    public long getTime(){
        return time;
    }
    public Runnable getRunnable() {
        return runnable;
    }
    @Override
    public int compareTo(MyTimerTask o) {
        return (int) (this.time - o.time);
    }
}
public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> map = new HashMap<>();

    }
}
